home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE08 / EXCEPT / HEX.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-01-05  |  804 b   |  34 lines

  1. unit Hex;
  2. interface
  3. uses SysUtils;
  4.  
  5. Type
  6.   EHexValError = class(Exception);
  7.  
  8.   EBadHexString = class(EHexValError);
  9.   EHexConversion= class(EHexValError);
  10.  
  11.  
  12.   function HexVal(HexStr: String): LongInt;
  13.  
  14. implementation
  15.  
  16.   function HexVal(HexStr: String): LongInt;
  17.   var i: Integer;
  18.   begin
  19.     { pre-condition: HexStr contains only ['0'..'9','A'..'F'] }
  20.     for i:=1 to Length(HexStr) do
  21.     begin
  22.       HexStr[i] := UpCase(HexStr[i]);
  23.       if not (HexStr[i] in ['0'..'9','A'..'F']) then
  24.         raise EBadHexString.Create('Bad HexStr "'+HexStr+'" passed.')
  25.     end;
  26.     { convert HexStr to LongInt }
  27.     Val('$'+HexStr,Result,i);
  28.     if i <> 0 then
  29.       raise EHexConversion.Create(
  30.         Format('Error at position: %d of HexStr %s',[i,HexStr]))
  31.   end {HexVal};
  32.  
  33. end.
  34.